# 需要开启这个参数，https://github.com/jemalloc/jemalloc/issues/1328
NACOS_HOME=${DEFAULT_NACOS_HOME:-'/opt/nacos'}
mkdir -p ${NACOS_HOME}/data

docker stop nacos && docker rm nacos
# NACOS_AUTH_TOKEN NACOS_AUTH_IDENTITY_KEY NACOS_AUTH_IDENTITY_VALUE 这三个在 2.2.0.1 后无默认值
# 这里我们将随机值设置到这三个参数上，防止与官网demo一致
docker run -itd --name nacos --restart=always -p 8848:8848 -p 9848:9848 \
 -e MODE=standalone -e NACOS_AUTH_ENABLE=true \
 -e NACOS_AUTH_TOKEN=SecretKey617185142951348809626854148457035447237663549219935682458480 \
 -e NACOS_AUTH_IDENTITY_KEY=HECQhnRQs7BPAKS4 -e NACOS_AUTH_IDENTITY_VALUE=yEC7GdeebdLnsJZ2 \
 -v ${NACOS_HOME}/data:/home/nacos/data \
 -v ${NACOS_HOME}/logs:/home/nacos/logs \
 nacos/nacos-server:v2.3.2

docker logs -f nacos



# 修改nacos用户的默认密码
function update_nacos_password() {
    # 确保存在jq，解析json字符串使用的工具
    yum install -y jq
    nacos_username=nacos
    old_password=nacos
    new_password=zpO8Lxe4pNPPoFM2
    login_info=$(curl -X POST -d "username=${nacos_username}&password=${old_password}" -H "Content-Type: application/x-www-form-urlencoded" http://localhost:8848/nacos/v1/auth/users/login)
    echo "$login_info"
    access_token=$(echo "$login_info" | jq -r '.accessToken')
    echo "$access_token"
    curl -X PUT -d "username=${nacos_username}&newPassword=${new_password}" \
     -H "Content-Type: application/x-www-form-urlencoded" -H "Accesstoken: ${access_token}" \
     http://localhost:8848/nacos/v1/auth/users?accessToken="${access_token}"
}
